package jinyilw.swing.rlaf.ui;

import jinyilw.swing.FontTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.ButtonModel;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicGraphicsUtils;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.plaf.basic.BasicMenuItemUI;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

/**
 * @author Jinyi
 */
public class RMenuItemUI extends BasicMenuItemUI
{

	private final RenderingConfig renderingConfig = new RenderingConfig();

	public static ComponentUI createUI(JComponent c)
	{
		return new RMenuItemUI();
	}

	@Override
	public void installUI(JComponent c)
	{
		super.installUI(c);
		menuItem.setOpaque(false);
		menuItem.setBorderPainted(false);
		menuItem.setFont(FontTools.defaultFont);
		selectionBackground = RThemeColor.darkThemeColor;
		selectionForeground = Color.white;
	}

	@Override
	public void update(Graphics g, JComponent c)
	{
		if (c.getClientProperty(BasicHTML.propertyKey) != null)
			RThemeColor.customForegroundSetting(c);
		Graphics2D g2d = (Graphics2D) g;
		renderingConfig.setTextAADrawing(g2d);
		paint(g, c);
		renderingConfig.resetTextAADrawing(g2d);
	}

	@Override
	protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect,
			String text)
	{
		RMenuItemUI.paintMenuText(g, menuItem, textRect, text,
				selectionForeground);
	}

	@Override
	protected void paintBackground(Graphics g, JMenuItem menuItem,
			Color bgColor)
	{
		RMenuItemUI.paintMenuBackground(g, menuItem, bgColor);
	}

	static void paintMenuText(Graphics g, JMenuItem menuItem,
			Rectangle textRect, String text, Color selectionForeground)
	{
		if (text == null || text.isEmpty())
			return;
		ButtonModel model = menuItem.getModel();
		FontMetrics fm = g.getFontMetrics();
		int menuIndex = menuItem.getDisplayedMnemonicIndex();

		if (model.isEnabled())
		{
			if (model.isArmed()
					|| (menuItem instanceof JMenu && model.isSelected()))
				g.setColor(selectionForeground);
			else
			{
				Color fColor = menuItem.getForeground();
				if (fColor == null || fColor instanceof UIResource)
					g.setColor(RThemeColor.colorSet.textColor);
				else
					g.setColor(fColor);
			}
			BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, menuIndex,
					textRect.x, textRect.y + fm.getAscent());
		} else
		{
			g.setColor(RThemeColor.colorSet.transBGColor);
			g.drawString(text, textRect.x + 1, textRect.y + fm.getAscent() + 1);
			g.setColor(RThemeColor.colorSet.unableTextColor);
			g.drawString(text, textRect.x, textRect.y + fm.getAscent());
		}
	}

	static void paintMenuBackground(Graphics g, JMenuItem menuItem,
			Color bgColor)
	{
		ButtonModel model = menuItem.getModel();
		Color oldColor = g.getColor();
		if (model.isArmed()
				|| (menuItem instanceof JMenu && model.isSelected()))
		{
			g.setColor(RThemeColor.darkThemeColor);
			g.fillRect(0, 0, menuItem.getWidth(), menuItem.getHeight());
			g.setColor(oldColor);
		}
	}
}
